__author__ = 'wangleijie'
from xml.sax.handler import ContentHandler
from xml.sax import parse

class TestHandler(ContentHandler):
    is_heading = False
    def startElement(self, name, attrs):
        if name == "ul":
            self.is_heading = True
    def endElement(self, name):
        if name == "ul":
            self.is_heading = False
    def characters(self, content):
        if self.is_heading:
            print content

class HeadlineHandler(ContentHandler):
    def __init__(self, HeadContent):
        ContentHandler.__init__(self)
        self.HeadContent = HeadContent
        self.HeadContent["name"]= ""
        self.HeadContent["id"] = ""
        self.HeadContent["desc"] = ""
        self.HeadContent["unkwon"] = ""
        self.data = []
        self.inAutoTest = False;
    def startElement(self, name, attrs):
        if name == "auto_test":
            for i in attrs.keys():
                if i == "name":
                    self.HeadContent["name"] = attrs[i]
                elif i == "id":
                    self.HeadContent["id"] = attrs[i]
                elif i == "desc":
                    self.HeadContent["desc"] = attrs[i]
                else:
                    self.HeadContent["unkwon"] = i + ": " + attrs[i]
            a="start test : " + name
            self.inAutoTest = True
    def endElement(self, name):
        if name == "auto_test":
            a="end test: "  + name
    def characters(self, content):
        if self.inAutoTest :
            self.data.append(content)
class HtmlPageMaker(ContentHandler):pass
class MakeDownPageMaker(ContentHandler):pass


headline = {}
parse("auto_test.xml", HeadlineHandler(headline))
print headline

class Dispatcher:
    def startElement(self, name, attrs):
        self.dispatch('start', name, attrs)
    def endElement(self, name, attrs):
        self.dispatch('end', name)
    def dispatch(self, prefix, name, attrs=None):
        mname = prefix + name.capitalize()
        dname = 'default' + prefix.capitalize()
        method = getattr(self, mname, None)
        if callable(method):
            args = ()
        else:
            method = getattr(self, dname, None)
            args = name
        if prefix == 'start' :
            args += attrs
        if callable(method):
            method(*args)